package com.bluesky.plum.domain.repository.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.bluesky.plum.domain.Person;
import com.bluesky.plum.domain.repository.BaseRepository;
import com.bluesky.plum.domain.repository.Repository;
import com.bluesky.plum.domain.repository.search.condition.Condition;

public class EnumRepository<T> extends BaseRepository<T> implements Repository<T> {

	protected List<T> list;

	public EnumRepository(Class enumClazz) {

		try {

			T[] rslt = (T[]) enumClazz.getEnumConstants();

			list = new ArrayList<T>();

			for (T t : rslt)
				list.add(t);

		} catch (Exception e) {
			throw new RuntimeException(e);

		}

	}

	@Override
	public void add(T entity) {
		throw new RuntimeException("Do not support");

	}

	@Override
	public List<T> getAll() {

		return list;
	}

	@Override
	public T getByIndex(int index) {

		return list.get(index);
	}

	@Override
	public List<T> getByPage(int pageLength, int pageNumber) {
		List<T> rslt = new ArrayList<T>();

		int start = pageLength * pageNumber;
		if (start >= getCount())
			throw new RuntimeException("the page do not exist");

		int end = start + pageLength;
		if (end >= list.size())
			end = list.size();

		for (int i = start; i < end; i++)
			rslt.add(list.get(i));

		return rslt;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public void remove(T entity) {
		throw new RuntimeException("Do not support");

	}

	@Override
	public void update(T oldEntity, T newEntity) {
		throw new RuntimeException("Do not support");

	}

	@Override
	public List<T> find(Condition condition) {
		// TODO Auto-generated method stub
		return null;
	}
}
